package com.autonavi.abs.common.api;

import com.autonavi.abs.common.enumeration.ResponseCodeEnum;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: 统一响应</p>
 * @date 2020-07-20 15:59
 */
public class ServiceResponse<T> {

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应描述
     */
    private String message;
    /**
     * 响应内容
     */
    private T data;

    public ServiceResponse() {
    }

    public ServiceResponse(int code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private static <T> ServiceResponse response(int code, String msg, T data) {
        ServiceResponse resp = new ServiceResponse();
        resp.setCode(code);
        resp.setMessage(msg);
        resp.setData(data);
        return resp;
    }

    /**
     * 参数缺失
     *
     * @return
     */
    public static ServiceResponse paramEmpty() {
        return response(ResponseCodeEnum.PARAM_EMPTY.getCode(), ResponseCodeEnum.PARAM_EMPTY.getDesc(), null);
    }

    /**
     * 参数缺失
     *
     * @param message
     * @return
     */
    public static ServiceResponse paramEmpty(String message) {
        return response(ResponseCodeEnum.PARAM_EMPTY.getCode(), message, null);
    }

    /**
     * 参数不合法
     *
     * @return
     */
    public static ServiceResponse paramIllegal() {
        return response(ResponseCodeEnum.PARAM_ILLEGAL.getCode(), ResponseCodeEnum.PARAM_ILLEGAL.getDesc(), null);
    }

    /**
     * 参数不合法
     *
     * @param message
     * @return
     */
    public static ServiceResponse paramIllegal(String message) {
        return response(ResponseCodeEnum.PARAM_ILLEGAL.getCode(), message, null);
    }

    /**
     * 服务异常
     *
     * @return
     */
    public static ServiceResponse serverError() {
        return response(ResponseCodeEnum.INTERFACE_EXCEPTION.getCode(), ResponseCodeEnum.INTERFACE_EXCEPTION.getDesc(), null);
    }

    /**
     * 服务异常
     *
     * @param message
     * @return
     */
    public static ServiceResponse serverError(String message) {
        return response(ResponseCodeEnum.INTERFACE_EXCEPTION.getCode(), message, null);
    }

    /**
     * 请求失败
     *
     * @param responseCodeEnum
     * @return
     */
    public static ServiceResponse fail(ResponseCodeEnum responseCodeEnum) {
        return response(responseCodeEnum.getCode(), responseCodeEnum.getDesc(), null);
    }

    /**
     * 请求失败
     *
     * @param code
     * @param message
     * @return
     */
    public static ServiceResponse fail(int code, String message) {
        return response(code, message, null);
    }

    /**
     * 请求失败
     *
     * @param code
     * @param message
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ServiceResponse fail(int code, String message, T result) {
        return response(code, message, result);
    }

    /**
     * 请求成功
     *
     * @return
     */
    public static ServiceResponse success() {
        return response(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getDesc(), null);
    }

    /**
     * 请求成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServiceResponse success(T data) {
        return response(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getDesc(), data);
    }

    /**
     * 请求成功
     *
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServiceResponse success(String message, T data) {
        return response(ResponseCodeEnum.SUCCESS.getCode(), message, data);
    }

    public boolean isSuccess() {
        return this.code == ResponseCodeEnum.SUCCESS.getCode();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

}
